package com.xyazm.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xyazm.product.entity.AttrEntity;
import com.xyazm.product.entity.AttrGroupEntity;
import com.xyazm.product.entity.vo.AttrGroupRelationVo;
import com.xyazm.product.entity.vo.AttrGroupWithAttrsVo;
import com.xyazm.product.service.AttrAttrgroupRelationService;
import com.xyazm.product.service.AttrGroupService;
import com.xyazm.product.service.AttrService;
import com.xyazm.product.service.CategoryService;
import com.xyazm.common.utils.PageUtils;
import com.xyazm.common.utils.R;



/**
 * 属性分组
 *
 * @author xuyou
 * @date 2020-06-11 17:15:45
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
   
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService relationService;
    
    /**
     * 批量添加分组和属性的关联关系
     * @Title: addRelation
     * @Description: 
     * @Author xuyou
     * @DateTime 2020年6月26日 下午10:45:32
     * @param vos
     * @return
     */
    ///product/attrgroup/attr/relation
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){

        relationService.saveBatch(vos);
        return R.ok();
    }
    
    /**
     * 根据分类id查询属性分组信息和该分组对应的属性信息
     * @Title: getAttrGroupWithAttrs
     * @Description: 
     * @Author xuyou
     * @DateTime 2020年6月27日 上午11:08:44
     * @param catelogId
     * @return
     */
    ///product/attrgroup/{catelogId}/withattr
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId")Long catelogId){

        //1、查出当前分类下的所有属性分组，
        //2、查出每个属性分组的所有属性
       List<AttrGroupWithAttrsVo> vos =  attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
       return R.ok().put("data",vos);
    }
    
    /**
     * 根据属性分组id获取该分组下的所有属性
     * @Title: attrRelation
     * @Description: 
     * @Author xuyou
     * @DateTime 2020年6月26日 下午10:27:56
     * @param attrgroupId
     * @return
     */
    ///product/attrgroup/{attrgroupId}/attr/relation
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> entities =  attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",entities);
    }
    
    /**
     * 根据属性分组id分页获取该分组下的所有属性(排除已经拥有的)
     * @Title: attrNoRelation
     * @Description: 
     * @Author xuyou
     * @DateTime 2020年6月26日 下午10:32:43
     * @param attrgroupId
     * @param params
     * @return
     */
    ///product/attrgroup/{attrgroupId}/noattr/relation
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params){
        PageUtils page = attrService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("page",page);
    }

    /**
     * 批量删除分组关联的属性
     * @Title: deleteRelation
     * @Description: 
     * @Author xuyou
     * @DateTime 2020年6月26日 下午10:34:14
     * @param vos
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody  List<AttrGroupRelationVo> vos){
        attrService.deleteRelation(vos);
        return R.ok();
    }
    /**
     * 列表
     *    {
		   page: 1,//当前页码
		   limit: 10,//每页记录数
		   sidx: 'id',//排序字段
		   order: 'asc/desc',//排序方式
		   key: '华为'//检索关键字
		 }
		 
		 catelogId 三级分类id
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,
    		@PathVariable("catelogId") Long catelogId){
        
        PageUtils page = attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
		Long catelogId = attrGroup.getCatelogId();
		List<Long> catelogPath = categoryService.findCatelogPath(catelogId);
		attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		
		attrGroupService.save(attrGroup);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		
		attrGroupService.updateById(attrGroup);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
        return R.ok();
    }

}
